//
//  IFTieMessageView.m
//  IfengNews
//
//  Created by Ryan on 14-3-6.
//
//

#import "IFTieMessageView.h"
#import "CLGloble.h"
#define IFTieMessageView_message_size 16.f
#define IFTieMessageView_subMessage_size 13.f

@interface IFTieMessageView ()

@property (nonatomic,strong) UIImageView *icon;
@property (nonatomic,strong) UILabel *messageLabe;
@property (nonatomic,strong) UILabel *subMessageLabe;
@property (nonatomic,strong) UIActivityIndicatorView *indicator;

@end

@implementation IFTieMessageView

- (instancetype)initWith:(IFTieMessageViewStyle)style message:(NSString *)message subMessage:(NSString *)submessage{
    self=[super init];
    if(self){
        float labelWith=95.f;
        float sublabelLength=16.f;
        
        //self.image=[UIImage imageNamed:ICurrentTheme.modelViewStyle.blockerViewBgImg];
        self.image=[UIImage imageNamed:@"blocker_view_bg.png"];
        if(style!=IFTieMessageViewStyleCancelLoading){
            self.icon=[[UIImageView alloc] init];
            self.icon.frame=CGRectMake(8, 9, 49, 49);
            self.icon.image=[self iconImage:style];
            [self addSubview:self.icon];
        }else{
            self.indicator=[[UIActivityIndicatorView alloc] init];
            self.indicator.frame=CGRectMake(8, 9, 49, 49);
            [self.indicator startAnimating];
            [self addSubview:self.indicator];
            
        }
        
        
        if(_isStrNotNull(message)){
        UIFont *font=[UIFont systemFontOfSize:IFTieMessageView_message_size];
        CGSize size=[message sizeWithFont:font];
            labelWith=size.width;
            if(labelWith>200.f)labelWith=200.f;
            if(labelWith<95.f)labelWith=95.f;
            
            _messageLabe=[[UILabel alloc] init];
            _messageLabe.frame=CGRectMake(65, 19, labelWith, 17);
            _messageLabe.font=font;
            _messageLabe.text=message;
            _messageLabe.textColor=[UIColor whiteColor];
            _messageLabe.backgroundColor=[UIColor clearColor];
            [self addSubview:_messageLabe];
        }
        
        if(_isStrNotNull(submessage)){
            UIFont *sfont=[UIFont systemFontOfSize:IFTieMessageView_subMessage_size];
            CGSize size=[submessage sizeWithFont:sfont];
            sublabelLength=((int)size.width%(int)labelWith)==0?
            (int)size.width/(int)labelWith:((int)size.width/(int)labelWith+1)*sfont.lineHeight;
            _subMessageLabe=[[UILabel alloc] init];
            _subMessageLabe.frame=CGRectMake(65, 19+17+4, labelWith, sublabelLength);
            _subMessageLabe.backgroundColor=[UIColor clearColor];
            _subMessageLabe.font=sfont;
            _subMessageLabe.text=submessage;
            _subMessageLabe.textColor=[UIColor whiteColor];
            [self addSubview:_subMessageLabe];
        }
        
        float width=_messageLabe.frame.origin.x+labelWith+8;
        float height=_subMessageLabe?_subMessageLabe.frame.origin.y+_subMessageLabe.frame.size.height:0;
        if(height<76.f)height=76.f;
        
        self.frame=CGRectMake(0, 0, width, height);
    }
    return self;
}

- (void)show{
//    UIWindow *window=[self getMainWindow];

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if (!window) {
        window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    self.center=CGPointMake(window.frame.size.width/2.f, (window.frame.size.height/2.f)-75.f);
    [window addSubview:self];
    self.alpha=0.f;
    [UIView animateWithDuration:0.2f animations:^{
        self.alpha=1.f;
    } completion:^(BOOL finish){
        [self performSelector:@selector(hidden) withObject:Nil afterDelay:0.3];
    
    }];
}

- (void)shwoInRect:(CGRect)rect{
    UIWindow *window=[[CLGloble sharedInstance] getMainWindow];
    self.frame=rect;
    [window addSubview:self];
    
    self.alpha=0.f;
    [UIView animateWithDuration:0.2f animations:^{
        self.alpha=1.f;
    } completion:^(BOOL finish){
        [self performSelector:@selector(hidden) withObject:Nil afterDelay:0.3];
        
    }];

}

- (void)justShow{
    UIWindow *window=[[CLGloble sharedInstance] getMainWindow];
    self.center=CGPointMake(window.frame.size.width/2.f, (window.frame.size.height/2.f)-75.f);
    [window addSubview:self];
    self.alpha=0.f;
    [UIView animateWithDuration:0.2f animations:^{
        self.alpha=1.f;
    } completion:^(BOOL finish){
        //[self performSelector:@selector(hidden) withObject:Nil afterDelay:0.3];
        
    }];


}

- (void)hiddenAndRemove{
    [self hidden];
}


- (void)hidden{
    [UIView animateWithDuration:0.2f animations:^{
        self.alpha=0.f;
    } completion:^(BOOL finish){
        [self removeFromSuperview];
    }];
}





- (UIImage *)iconImage:(int)sytle{
    UIImage *img=nil;
    switch (sytle) {
        case IFTieMessageViewStyleSuccess:
            img=[UIImage imageNamed:@"blocker_view_success_alert.png"];
            break;
            
        case IFTieMessageViewStyleAlert:
            img=[UIImage imageNamed:@"blocker_view_fail_alert.png"];
            break;
            
        case IFTieMessageViewStyleCancelCollect:
            img=[UIImage imageNamed:@"blocker_view_cancel_collect.png"];
            break;
            
     
    }
    
    return img;
}



@end
